


//
//  ZYAttendanceRecordController.m
//  优学荟管理系统
//
//  Created by edz on 2017/12/5.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYAttendanceRecordController.h"
#import "ZYAttendanceRecordCell.h"
#import "ZYAttendanceRecordModel.h"
#define kCell @"cell"
#import "JXAlertview.h"
#import "CustomDayDatePicker.h"
#import "HMTeacherAttendanceRecordVC.h"
@interface ZYAttendanceRecordController ()<UITableViewDelegate,UITableViewDataSource,CustomAlertDelegete>
{
    NSInteger _mark;//标记类型
    NSString *_selectDate;//标记选中的日期
    
}

@property (nonatomic,strong) CustomDayDatePicker *datePicker;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) UILabel *startLabel;//开始时间
@property (nonatomic,strong) UIButton *startSelectButton;//选择开始时间
@property (nonatomic,strong) UILabel *endLabel;//结束时间
@property (nonatomic,strong) UIButton *endSelectButton;//选择结束时间

@property (nonatomic,strong) UIButton *sureButton;//确定

@property (nonatomic,strong) UIView *allView;
@property (nonatomic,assign) BOOL isTrue;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,assign) BOOL isFirst;
@property (nonatomic,copy) NSString *firstData;
@property (nonatomic,copy) NSString *startStr;
@property (nonatomic,copy) NSString *endStr;
@property(nonatomic,copy) NSString *lastData;
@end

@implementation ZYAttendanceRecordController


- (NSMutableArray *)dataSource{
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.title = @"考勤记录";
    
    self.lastData = [NSString currentDateTow];
    [self setNetWork];
    [self setUpSuperView];
    self.view.backgroundColor = ColorRGB(247, 245, 245);
   
    

}










#pragma mark --- 网络请求方法的实现
- (void)setNetWork{
    
    //取出临时保存的登录信息
    
//http://192.168.0.113:8080/StarShine/ys/yxhUser.do?action=getTeacherRecordByOid&oid=1
  
    NSString *oid = [AccountTool shareAccountTool].currentAccount.oid;
    //    self.page = 1;
    NSDate *date =[NSDate date];//简书 FlyElephant
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"yyyy"];
    NSInteger currentYear=[[formatter stringFromDate:date] integerValue];
    [formatter setDateFormat:@"MM"];
    NSInteger currentMonth=[[formatter stringFromDate:date]integerValue];
    [formatter setDateFormat:@"dd"];
//    NSInteger currentDay=[[formatter stringFromDate:date] integerValue];
    
    
    NSString *yy = [NSString stringWithFormat:@"%ld",currentYear];
    
    NSString *mm = [yy stringByAppendingFormat:@"-%ld",currentMonth];
    
//    NSString *dd = [mm stringByAppendingFormat:@"-%ld",currentDay];
//
//    NSLog(@"%@",dd);
    
    self.firstData = [NSString stringWithFormat:@"%@-01",mm];
    NSString *url;
    if (self.isFirst) {
        
         url = [NSString stringWithFormat:@"yxhUser.do?action=getTeacherRecordByOid&oid=%@&startTime=%@&endTime=%@",oid,self.startStr,self.endStr];
        
        
    }else{
        
        
        
       url = [NSString stringWithFormat:@"yxhUser.do?action=getTeacherRecordByOid&oid=%@&startTime=%@&endTime=%@",oid,self.firstData,self.lastData];
        
    }
    
   
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [SVProgressHUD dismiss];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {

            [self.dataSource removeAllObjects];
            
            NSArray *arr = [Obj valueForKey:@"t"];
            
            for (NSDictionary *dic in arr) {
                
                ZYAttendanceRecordModel *model = [ZYAttendanceRecordModel new];
                // model.uid = [dic valueForKey:@"uid"];
                [model setValuesForKeysWithDictionary:dic];
                
                [self.dataSource addObject:model];
                
                
            }
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
        }
        
        
        
    } Fail:^(NSError *Error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
    
    
    
    
    
}



































- (CustomDayDatePicker *)datePicker {
    
    if (_datePicker == nil) {
        self.datePicker = [[CustomDayDatePicker alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth - 60, 140)];
        
    }
    
    
    return _datePicker;
}

- (void)setUpSuperView{
    self.allView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,45)];
    self.allView.backgroundColor = ColorRGB(247, 245, 245);
    [self.view addSubview:self.allView];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.allView.frame.origin.y + self.allView.frame.size.height, kScreenWidth, kScreenHeight - 64)];
    self.tableView.backgroundColor = ColorRGB(247, 245, 245);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[ZYAttendanceRecordCell class] forCellReuseIdentifier:kCell];
    
    self.tableView.tableFooterView = [UIView new];

    self.tableView.estimatedRowHeight = 200;
    
   
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    

  
    
    UILabel *lineLabel = [UILabel new];
    lineLabel.backgroundColor = [UIColor blackColor];
    [self.allView addSubview:lineLabel];
    
    [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.left.offset(kScreenWidth/2 - 15);
        make.centerY.equalTo(self.allView);
        make.width.offset(30);
        make.height.offset(2);
    
        
    }];
    
    
        self.startSelectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    //    self.startSelectButton.frame = CGRectMake(0, 5, kScreenWidth/2 -20, 35);
        [self.startSelectButton setImage:[UIImage imageNamed:@"icon_more"] forState:(UIControlStateNormal)];
        [self.startSelectButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    //    self.startSelectButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.startSelectButton setTitleColor:ColorRGB(102, 102, 102) forState:(UIControlStateNormal)];
    //    self.startSelectButton.backgroundColor = [UIColor redColor];
        [self.startSelectButton setTitle:[NSString stringWithFormat:@"%@",self.firstData] forState:(UIControlStateNormal)];
        self.startSelectButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        self.startSelectButton.imageEdgeInsets = UIEdgeInsetsMake(0,120,0, 0);
        self.startSelectButton.titleEdgeInsets = UIEdgeInsetsMake(0,0, 0, 30);
        [self.allView addSubview:self.startSelectButton];
//    self.startSelectButton.backgroundColor = [UIColor redColor];
    
        [self.startSelectButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(lineLabel.mas_left).offset(-10);
            make.centerY.equalTo(self.allView);
            make.width.offset(160);
        }];
    
    
    
    
    
    
    
    
   
  
    
    
    
    
    
    
    
    
    
    
    
    self.endSelectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    //    self.startSelectButton.frame = CGRectMake(0, 5, kScreenWidth/2 -20, 35);
    [self.endSelectButton setImage:[UIImage imageNamed:@"icon_more"] forState:(UIControlStateNormal)];
    [self.endSelectButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
     self.endSelectButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    self.endSelectButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.endSelectButton setTitleColor:ColorRGB(102, 102, 102) forState:(UIControlStateNormal)];
//    self.endSelectButton.imageEdgeInsets = UIEdgeInsetsMake(0,100,0, 0);
//    self.endSelectButton.titleEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
//    self.endSelectButton.backgroundColor = [UIColor redColor];
    [self.endSelectButton setTitle:[NSString stringWithFormat:@"%@",self.lastData] forState:(UIControlStateNormal)];
    self.endSelectButton.imageEdgeInsets = UIEdgeInsetsMake(0,110,0, 0);
    self.endSelectButton.titleEdgeInsets = UIEdgeInsetsMake(0,0, 0, 30);
    [self.allView addSubview:self.endSelectButton];

    [self.endSelectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineLabel.mas_right).offset(10);
        make.centerY.equalTo(self.allView);
        make.width.offset(160);
    }];


    
    
    
    
    
    
    
    
//    self.startLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,15, kScreenWidth - 140, 20)];
//    self.startLabel.text = @"开始时间";
//    self.startLabel.textColor = ColorRGB(127, 125, 125);
//    self.startLabel.font = [UIFont systemFontOfSize:14];
//    [self.allView addSubview:self.startLabel];
//
//
//    self.startSelectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    self.startSelectButton.frame = CGRectMake(kScreenWidth - 140,self.startLabel.frame.origin.y,120,20);
//    [self.startSelectButton setImage:[UIImage imageNamed:@"icon_time_default"] forState:(UIControlStateNormal)];
//    self.startSelectButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
//    [self.startSelectButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
//
//    [self.startSelectButton setTitle:[NSString stringWithFormat:@"%@",self.firstData] forState:(UIControlStateNormal)];
//
//
//
//    self.startSelectButton.titleLabel.font = [UIFont systemFontOfSize:14];
//    self.startSelectButton.backgroundColor = ColorRGB(235, 250, 243);
//    [self.allView addSubview:self.startSelectButton];
//
//
//    self.endLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.startLabel.frame.origin.x,self.startLabel.frame.origin.y + self.startLabel.frame.size.height + 15,self.startLabel.frame.size.width , self.startLabel.frame.size.height)];
//    self.endLabel.text = @"结束时间";
//    self.endLabel.textColor = ColorRGB(127, 125, 125);
//    self.endLabel.font = [UIFont systemFontOfSize:14];
//    [self.allView addSubview:self.self.endLabel];
//
//    self.endSelectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    self.endSelectButton.frame = CGRectMake(self.startSelectButton.frame.origin.x,self.endLabel.frame.origin.y,self.startSelectButton.frame.size.width,self.startSelectButton.frame.size.height);
//    [self.endSelectButton setImage:[UIImage imageNamed:@"icon_time_default"] forState:(UIControlStateNormal)];
//    self.endSelectButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
//    [self.endSelectButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
//
//
//    [self.endSelectButton setTitle:[NSString stringWithFormat:@"%@",[NSString yearMonthDay]] forState:(UIControlStateNormal)];
//    self.endSelectButton.titleLabel.font = [UIFont systemFontOfSize:14];
//    self.endSelectButton.backgroundColor = ColorRGB(235, 250, 243);
//    [self.allView addSubview:self.endSelectButton];
//
//
//    self.sureButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    self.sureButton.frame = CGRectMake(30, self.endSelectButton.frame.origin.y + self.endSelectButton.frame.size.height  + 30, kScreenWidth - 60, 30);
//    [self.sureButton setTitle:@"确定" forState:(UIControlStateNormal)];
//
//    [self.sureButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
//    self.sureButton.backgroundColor = ColorRGB(49, 204, 131);
//    self.sureButton.layer.cornerRadius = 15;
//    self.sureButton.clipsToBounds = YES;
//    [self.allView addSubview:self.sureButton];
//
//
//    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.sureButton.frame.origin.y + self.sureButton.frame.size.height + 15, kScreenWidth, 3)];
//    lineLabel.backgroundColor = ColorRGB(245, 245, 245);
//    [self.allView addSubview:lineLabel];
//
//
    self.startSelectButton.tag = 201;
    self.endSelectButton.tag = 202;
    [self.startSelectButton addTarget:self action:@selector(allButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.endSelectButton addTarget:self action:@selector(allButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
//
//    [self.sureButton addTarget:self action:@selector(sureButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
//
//
    
    
    
    
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYAttendanceRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    if (self.dataSource.count > indexPath.row) {
        
        ZYAttendanceRecordModel *model = self.dataSource[indexPath.row];
        
        [cell assignByZYAttendanceRecordModel:model];
    }
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYAttendanceRecordModel *model = self.dataSource[indexPath.row];
    HMTeacherAttendanceRecordVC *vc = [HMTeacherAttendanceRecordVC new];
    vc.isJiGou1 = YES;
    vc.isJiGou = [NSString stringWithFormat:@"%@",model.uid];
    vc.teacherName = model.teacherName;
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}





#pragma mark ---详情方法的实现
- (void)allButtonAction:(UIButton *)sender{
    
    NSInteger tag = sender.tag;
    
    if (201 == tag) {
        
        NSLog(@"dd");
        
        JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 60, 200)];
        alert.center = self.view.center;
        alert.delegate = self;
        [alert initwithtitle:@"开始时间" andcommitbtn:@"确定" andStr:@"0"];
        [alert addSubview:self.datePicker];
        [alert show];
        
        
        _mark = 1;
        
        
        
    }else{
        
        JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 60, 200)];
        alert.center = self.view.center;
        alert.delegate = self;
        [alert initwithtitle:@"结束时间" andcommitbtn:@"确定" andStr:@"0"];
        [alert addSubview:self.datePicker];
        [alert show];
        
        
        _mark = 2;
        
        
    }
    
    
    
    
    
}




- (void)btnindex:(int)index :(int)tag{
    
    if (index == 2) {
        if (_mark == 1) {
            _selectDate = [NSString stringWithFormat:@"%d-%02d-%02d",self.datePicker.year,self.datePicker.month,self.datePicker.day];
            [self.startSelectButton setTitle:[NSString stringWithFormat:@"%@",_selectDate] forState:(UIControlStateNormal)];
        }else if (_mark == 2){
            
            _selectDate = [NSString stringWithFormat:@"%d-%02d-%02d",self.datePicker.year,self.datePicker.month,self.datePicker.day];
            [self.endSelectButton setTitle:[NSString stringWithFormat:@"%@",_selectDate] forState:(UIControlStateNormal)];
            
            [self sureButtonAction];
        }
    }
    
    
    
    
}





#pragma mark --- 确定方法的实现
- (void)sureButtonAction{
    
    NSString *start = [self.startSelectButton.currentTitle stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSString *endSelect = [self.endSelectButton.currentTitle stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    
    NSComparisonResult result = [start compare:endSelect];
    
    if (result != NSOrderedAscending) {
        [SVProgressHUD showErrorWithStatus:@"结束时间必须大于开始时间!"];
        return;
    }else{
        
       
        [self setSetTowSureStart:start endSelect:endSelect];
       
        
        
    }
    
    
    
}


- (void)setSetTowSureStart:(NSString *)startStr endSelect:(NSString *)endSelect{
    
    
    self.isFirst = YES;
    self.startStr = startStr;
    self.endStr = endSelect;
   
    [self setNetWork];
    
    
    
    
    
}


- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [SVProgressHUD dismiss];
}

//传入今天的时间，返回明天的时间
//- (NSString *)GetTomorrowDay:(NSDate *)aDate {
//    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
//    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
//    [components setDay:([components day]+1)];
//    
//    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
//    NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
//    [dateday setDateFormat:@"yyyy-MM-dd"];
//    return [dateday stringFromDate:beginningOfWeek];
//}



@end
